<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>支付宝支付返回界面</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }

        a {
            text-decoration: none;
        }

        .back_a {
            height: 50px;
            width: 126px;
            color: #71a6ee;
            font-size: 20px;
            background: rgba(158, 255, 215, 0.5);
            position: absolute;
            text-align: center;
            left: 46%;
            margin-top: 40px;
        }

        .back_a:hover {
            background: #ee6e40;
            color: white;
        }

    </style>
</head>
<%@ page import="java.util.*" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.alipay.api.internal.util.*" %>
<%@ page import="com.yoyo.itravel.constants.AlipayConfigConstant" %>
<%@ page import="com.yoyo.itravel.service.ISightOrderService" %>
<%@ page import="com.yoyo.itravel.service.impl.SightOrderServiceImpl" %>
<%@ page import="com.yoyo.itravel.service.ITicketTypeDetailService" %>
<%@ page import="com.yoyo.itravel.service.impl.TicketTypeDetailServiceImpl" %>
<%@ page import="com.yoyo.itravel.service.ISightService" %>
<%@ page import="com.yoyo.itravel.service.impl.SightServiceImpl" %>
<%@ page import="com.yoyo.itravel.bean.SightOrder" %>
<%@ page import="com.yoyo.itravel.bean.Sight" %>
<%

     /** 功能：支付宝服务器同步通知页面 **/

    ISightOrderService sightOrderService = new SightOrderServiceImpl();
    ITicketTypeDetailService ticketTypeDetailService = new TicketTypeDetailServiceImpl();
    ISightService sightService = new SightServiceImpl();

    //获取支付宝GET过来反馈信息
    Map<String, String> params = new HashMap<String, String>();
    Map<String, String[]> requestParams = request.getParameterMap();
    for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
        String name = (String) iter.next();
        String[] values = (String[]) requestParams.get(name);
        String valueStr = "";
        for (int i = 0; i < values.length; i++) {
            valueStr = (i == values.length - 1) ? valueStr + values[i]
                    : valueStr + values[i] + ",";
        }
        //乱码解决，这段代码在出现乱码时使用
        valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
        params.put(name, valueStr);
    }

    // 调用SDK验证签名
    boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfigConstant.ALIPAY_PUBLIC_KEY, AlipayConfigConstant.CHARSET, AlipayConfigConstant.SIGN_TYPE); //调用SDK验证签名

    String out_trade_no = null;
    String trade_no = null;
    String total_amount = null;

    // 判断是否验证成功
    if (signVerified) {
        // 订单号
        out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 支付宝交易号
        trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 付款金额
        total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");

        if (!StringUtils.isEmpty(trade_no)) {
            // 支付成功，修改订单状态
            sightOrderService.updateOrderStatus(out_trade_no, 2);
            // 添加订单交易号
            sightOrderService.addTradeNoByOrderId(out_trade_no, trade_no);
            // 对应的景点门票销量增加
            SightOrder sightOrder = sightOrderService.findByOrderId(out_trade_no);
            // 开启一个定时器--出行后修改订单状态为待点评
            sightOrderService.preCommentTimer(sightOrder);
            Sight sight = ticketTypeDetailService.findSight(sightOrder.getDetailTypeId());
            sightService.updateSellCount(sight.getSightId(), sightOrder.getOrderNum());
        }
    }
%>
<body>
<%--<c:forEach items="<%=params%>" var="map">--%>
    <%--<h2>${map.key}</h2>--%>
    <%--<h2>${map.value}</h2>--%>
<%--</c:forEach>--%>
    <c:choose>
        <c:when test="<%=signVerified %>">
            <div style="background: #94b7c4;width: 100%;height: 600px;">
                <h1><p style="color: #99ffb9;font-size: 40px;padding-top: 20px;margin: 0 auto;padding-left: 45%;">支付成功！</p></h1>
                <table style="margin: 0 auto;">
                    <tr>
                        <td width="120px;">
                            <span style="font-weight: bold;">订单号：</span>
                        </td>
                        <td height="70px" width="80px">
                            <label style="display: block;border: 2px solid #96ddff;border-radius: 10px;color: white;height: 27px;width: 298px;font-size: 16px;padding: 3px;">
                                <%=out_trade_no %>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span style="font-weight: bold;">支付宝交易号：</span>
                        </td>
                        <td height="70px">
                            <label style="display: block;border: 2px solid #96ddff;border-radius: 10px;color: white;height: 27px;width: 298px;font-size: 16px;padding: 3px;">
                                <%=trade_no %>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span style="font-weight: bold;">付款金额：</span>
                        </td>
                        <td height="70px">
                            <label style="display: block;border: 2px solid #96ddff;border-radius: 10px;color: white;height: 27px;width: 298px;font-size: 16px;padding: 3px;">
                                <%=total_amount %>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <a class="back_a" href="${pageContext.request.contextPath}/index.jsp">
                                <sapn style="line-height: 50px;">返回首页</sapn>
                            </a>
                        </td>
                    </tr>
                </table>
            </div>

        </c:when>
        <c:otherwise>
            <div style="background: #94b7c4;width: 100%;height: 600px;">
                <h1 style="margin: 0px auto;margin-left: 45%;padding-top: 90px;color: #ff1123;font-size: 40px;">交易失败！</h1>
                <div>
                    <a class="back_a" href="${pageContext.request.contextPath}/sight_order.jsp?detailTypeId=${detailTypeId }">
                        <sapn style="line-height: 50px;">返回订单页面</sapn>
                    </a>
                </div>
            </div>
        </c:otherwise>
    </c:choose>
</body>
</html>